#ifndef InterAddr_h
#define InterAddr_h
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string>

#define CONV(v) (struct sockaddr *)(v)
class InterAddr
{
public:
    void PortNet2Host()
    {
        _port = ::ntohs(_addr.sin_port);
    }

    void IpNet2Host()
    {
        char buffer[64];
        const char *ip = inet_ntop(AF_INET, &_addr.sin_addr, buffer, sizeof(buffer));
        _ip = ip;
    }

    InterAddr(sockaddr_in &sock) : _addr(sock)
    {
        PortNet2Host();
        IpNet2Host();
    }
    InterAddr(uint16_t port)
    {
        _addr.sin_family = AF_INET;
        _addr.sin_port = htons(port);
        _addr.sin_addr.s_addr = INADDR_ANY;
        PortNet2Host();
        IpNet2Host();
    }
    void Exchange(InterAddr addr)
    {
        _addr=addr._addr;
        _port=addr._port;
        _ip=addr._ip;
    }
    bool operator==(const InterAddr &addr) const
    {
        return _ip == addr._ip;
    }
    socklen_t NetAddrLen() { return sizeof(_addr); }
    struct sockaddr *NetAddr() { return CONV(&_addr); }
    uint16_t Port() { return _port; }
    std::string Ip() { return _ip; }

private:
    struct sockaddr_in _addr;
    uint16_t _port;
    std::string _ip;
};
#endif